package cn.xo68.portal.configuration;

import cn.xo68.boot.auth.core.common.AuthConstants;
import cn.xo68.boot.auth.server.properties.AuthServerProperties;
import cn.xo68.boot.cache.common.CacheConfiguration;
import cn.xo68.boot.cache.common.CacheConfigurer;
import cn.xo68.boot.cache.common.CacheConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class WxCacheConfigurer implements CacheConfigurer {

    @Autowired
    private AuthServerProperties authServerProperties;

    @Override
    public void addConfig(List<CacheConfiguration> configurations) {

        //客户端
        CacheConfiguration clientCacheConfiguration= CacheConfiguration.create(AuthConstants.CACHE_NAME_OAUTH2_CLIENT)
                .usePrefix(true)
                .cachePrefix(AuthConstants.CACHE_NAME_OAUTH2_CLIENT+AuthConstants.SPLIT_COLON)
                .timeoutSeconds(60*60*24*7)
                .build();
        configurations.add(clientCacheConfiguration);

        //客户端资源
        CacheConfiguration resourceCacheConfiguration= CacheConfiguration.create(AuthConstants.CACHE_NAME_OAUTH2_RESOURCE)
                .usePrefix(true)
                .cachePrefix(AuthConstants.CACHE_NAME_OAUTH2_RESOURCE+AuthConstants.SPLIT_COLON)
                .timeoutSeconds(60*60*24*7)
                .build();
        configurations.add(resourceCacheConfiguration);

        //角色资源
        CacheConfiguration roleResourceCacheConfiguration= CacheConfiguration.create(AuthConstants.CACHE_NAME_OAUTH2_ROLE_RESOURCE)
                .usePrefix(true)
                .cachePrefix(AuthConstants.CACHE_NAME_OAUTH2_ROLE_RESOURCE+AuthConstants.SPLIT_COLON)
                .timeoutSeconds(60*60*24*7)
                .build();
        configurations.add(roleResourceCacheConfiguration);

        //用户
        CacheConfiguration userCacheConfiguration= CacheConfiguration.create(AuthConstants.CACHE_NAME_OAUTH2_USER)
                .usePrefix(true)
                .cachePrefix(AuthConstants.CACHE_NAME_OAUTH2_USER+AuthConstants.SPLIT_COLON)
                .timeoutSeconds(60*60*24*7)
                .build();
        configurations.add(userCacheConfiguration);

        //令牌
        CacheConfiguration tokenCacheConfiguration= CacheConfiguration.create(AuthConstants.CACHE_NAME_OAUTH2_TOKEN)
                .usePrefix(true)
                .cachePrefix(AuthConstants.CACHE_NAME_OAUTH2_TOKEN+AuthConstants.SPLIT_COLON)
                .timeoutSeconds(authServerProperties.getTokenExpireSeconds())
                .build();
        configurations.add(tokenCacheConfiguration);

        //刷新令牌
        CacheConfiguration refreshTokenCacheConfiguration= CacheConfiguration.create(AuthConstants.CACHE_NAME_OAUTH2_REFRESH_TOKEN)
                .usePrefix(true)
                .cachePrefix(AuthConstants.CACHE_NAME_OAUTH2_REFRESH_TOKEN+AuthConstants.SPLIT_COLON)
                .timeoutSeconds(authServerProperties.getRefreshTokenExpireSeconds())
                .build();
        configurations.add(refreshTokenCacheConfiguration);

        //临时凭证
        CacheConfiguration AuthorizationCodeCacheConfiguration= CacheConfiguration.create(AuthConstants.CACHE_NAME_OAUTH2_AUTHORIZATION_CODE)
                .usePrefix(true)
                .cachePrefix(AuthConstants.CACHE_NAME_OAUTH2_AUTHORIZATION_CODE+AuthConstants.SPLIT_COLON)
                .timeoutSeconds(authServerProperties.getAuthorizeCodeExpireSeconds())
                .build();
        configurations.add(AuthorizationCodeCacheConfiguration);

        //登录失败次数
        CacheConfiguration loginFaiCacheConfiguration= CacheConfiguration.create(AuthConstants.CACHE_NAME_LOGIN_FAIL)
                .usePrefix(true)
                .cachePrefix(AuthConstants.CACHE_NAME_LOGIN_FAIL+AuthConstants.SPLIT_COLON)
                .timeoutSeconds(authServerProperties.getLoginFailPeriod()*60)
                .build();
        configurations.add(loginFaiCacheConfiguration);
    }

    @Override
    public CacheConfiguration getCacheDefault() {
        //默认保存30分钟
        CacheConfiguration cacheConfiguration= CacheConfiguration.create(CacheConstant.CACHE_NAME_DEFAULT)
                .usePrefix(true)
                .cachePrefix(CacheConstant.CACHE_PREFIX_DEFAULT)
                .timeoutSeconds(60*30)
                .build();
        return cacheConfiguration;
    }
}
